package net.arvin.mobilebanksystem.fragment;

import java.util.ArrayList;
import java.util.List;

import net.arvin.dao.ResponseHandler;
import net.arvin.mobilebanksystem.R;
import net.arvin.mobilebanksystem.activity.MainActivity;
import net.arvin.model.Card;
import net.arvin.model.User;
import net.arvin.service.CardService;
import net.arvin.utils.NetConnectionUtil;
import net.arvin.utils.PreferenceUtils;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

public class BindCardFragment extends Fragment implements OnClickListener {
	private EditText userName_et;
	private EditText cardNumber_et;
	private EditText password_et;
	private EditText repassword_et;
	private Button bindCard;
	private ResponseHandler mResponseHandler;
	private Card card = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_mine_bindcard, null);
		init(view);
		return view;
	}

	private void init(View view) {
		initData();
		initView(view);
		initListener();
	}

	private void initData() {
		card = new Card();
		User user = JSON.parseObject(
				PreferenceUtils.getPreferUserJson(getActivity()), User.class);
		card.setPhonenumber(user.getPhonenumber());
		// 默认银行卡里有1000元
		card.setMoney(1000f);
		card.setCardbank("移动手机银行");
		card.setCardtype("储蓄卡");
	}

	private void initListener() {
		bindCard.setOnClickListener(this);
	}

	private void initView(View view) {
		userName_et = (EditText) view.findViewById(R.id.userName_et);
		cardNumber_et = (EditText) view.findViewById(R.id.cardnumber_et);
		password_et = (EditText) view.findViewById(R.id.password_et);
		repassword_et = (EditText) view.findViewById(R.id.repassword_et);
		bindCard = (Button) view.findViewById(R.id.bindcard);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bindcard:
			dataCheck();
			break;
		}
	}

	private void dataCheck() {
		if (userNameCheck()) {
			return;
		}
		if (!cardNumberCheck()) {
			return;
		}
		if (!passwordCheck()) {
			return;
		}
		if (!repasswordCheck()) {
			return;
		}
		String cardJson = JSON.toJSONString(card);
		if (NetConnectionUtil.isConnectWithTip(getActivity(), "没有联网=_=!")) {
			CardService.tryBindCard(cardJson, getResponseHandler(),
					getActivity());
		}
	}

	private boolean userNameCheck() {
		String userName = userName_et.getText().toString().trim();
		if (userName == null || userName.equals("")) {
			Toast.makeText(getActivity(), "用户名不能为空~~", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		if (!userName.matches("^([\u4e00-\u9fa5]{2,4})&")) {
			Toast.makeText(getActivity(), "用户名只能为汉字", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		card.setUserName(userName);
		return true;
	}

	private boolean repasswordCheck() {
		String password = password_et.getText().toString().trim();
		String repassword = repassword_et.getText().toString().trim();
		if (!password.equals(repassword)) {
			Toast.makeText(getActivity(), "两次密码输入不一致~~", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		card.setCardpassword(password);
		return true;
	}

	private boolean passwordCheck() {
		String password = password_et.getText().toString().trim();
		if (password == null || password.equals("")) {
			Toast.makeText(getActivity(), "密码不能为空~~", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		if (password.length() != 6) {
			Toast.makeText(getActivity(), "密码长度为6位~~", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		if (!password.matches("^[0-9]+$")) {
			Toast.makeText(getActivity(), "密码必须是数字~~", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		return true;
	}

	private boolean cardNumberCheck() {
		String cardnumber = cardNumber_et.getText().toString().trim();
		if (cardnumber == null || cardnumber.equals("")) {
			Toast.makeText(getActivity(), "银行卡号不能为空~~", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		if (cardnumber.length() != 8) {
			Toast.makeText(getActivity(), "银行卡号长度不正确~~", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		if (!cardnumber.matches("^[0-9]+$")) {
			Toast.makeText(getActivity(), "银行卡号必须是数字~~", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		card.setCardnumber(cardnumber);
		return true;
	}

	private void savaCardsInfo() {
		List<Card> lists = JSON.parseArray(
				PreferenceUtils.getPreferCards(getActivity()), Card.class);
		if (lists == null) {
			lists = new ArrayList<Card>();
		}
		lists.add(card);
		PreferenceUtils.modifyStringValueInPreferences(PreferenceUtils.Cards,
				JSON.toJSONString(lists), getActivity());
	}

	private ResponseHandler getResponseHandler() {
		if (mResponseHandler == null) {
			mResponseHandler = new ResponseHandler() {
				@Override
				public void onRequsetSucceeded(final Object data) {
					Toast.makeText(getActivity(), "绑定成功~~", Toast.LENGTH_SHORT)
							.show();
					savaCardsInfo();

					PreferenceUtils.modifyBooleanValueInPreferences(
							PreferenceUtils.IsBindCard, true, getActivity());

					MainActivity.mainActivity.popBackStack();
				}

				@Override
				public void onRequestFailed(final Object data) {
					Toast.makeText(getActivity(), "该银行卡已被注册~~",
							Toast.LENGTH_SHORT).show();
				}
			};
		}
		return mResponseHandler;
	}
}
